//
//  MineHomeViewController.m
//  YYPHSpeedJump
//
//  Created by sujiang on 15/12/16.
//  Copyright © 2015年 sujiang. All rights reserved.
//

#import "MineHomeViewController.h"
#import "InstallViewController.h"
#import "BaseEffectView.h"
#import "LoginViewController.h"
#import "InvationCode.h"
#import "BaseNavigationController.h"


@interface MineHomeViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *icons;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) NSString *nickName;
@property (nonatomic, strong) NSArray *colorArr;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong)NSDictionary *dict;
@end
#define itemHeight [UIScreen mainScreen].bounds.size.width < 375?168*kHeightScale:([UIScreen mainScreen].bounds.size.width > 375? 182*kHeightScale :177*kHeightScale)
@implementation MineHomeViewController


- (void)viewWillAppear:(BOOL)animated
{
    
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidAppear:(BOOL)animated
{
    [self requestData];
    [self.collectionView reloadData];
}
- (void)requestData
{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"logined"]) {
        [Tools showAlert:@"您还未登录,请到个人登录!"];
        return;
    }
    
    NSString *myInfoUrl = [NSString stringWithFormat:@"%@%@",kBaseUrl,kMyInfoUrl];
    NSDictionary *dic = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"]};
    [NetworkHandle getInfoWithUrl:myInfoUrl dictionary:dic success:^(NSDictionary *dictionary) {
        self.dict = dictionary[@"data"];
        self.headerView.myIntegral.text = [NSString stringWithFormat:@"我的积分：%@",self.dict[@"integral"]];
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
    self.view.backgroundColor = kColors(239, 239, 244, 1);
    [self addSubViews];
    self.colorArr = @[kColors(249, 161, 132, 1), kColors(138, 199, 240, 1), kColors(108, 215, 181, 1), kColors(249, 157, 161, 1), kColors(249, 211, 98, 1), kColors(175, 223, 142, 1)];
   
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"logined"]) {
        BaseEffectView *baseView = [[BaseEffectView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [self.view addSubview:baseView];
        [baseView.loginBtn addTarget:self action:@selector(actionLoginBtn:) forControlEvents:(UIControlEventTouchUpInside)];
        [baseView.registBtn addTarget:self action:@selector(actionRegistBtn:) forControlEvents:(UIControlEventTouchUpInside)];
       // self.tableView.userInteractionEnabled = NO;
    }
    
}


- (NSMutableArray *)icons
{
    if (!_icons) {
        _icons = [NSMutableArray arrayWithObjects:@"sjdd", @"jfsc", @"wdsd", @"wdqb", nil];
    }
    return _icons;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        
        _dataArray = [NSMutableArray arrayWithObjects:@"速降订单", @"积分商城", @"我的晒单", @"我的钱包", nil];
    }
    return _dataArray;
}

- (void)setnavigation
{
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    UIButton *setBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    setBtn.frame = CGRectMake(0, 0, 44, 44);
    [setBtn setBackgroundImage:[UIImage imageNamed:@"sz"] forState:(UIControlStateNormal)];
    [aView addSubview:setBtn];
    [setBtn addTarget:self action:@selector(actionSet:) forControlEvents:(UIControlEventTouchUpInside)];
    
//    UIButton *messageBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    messageBtn.frame = CGRectMake(44, 0, 44, 44);
//    [messageBtn setBackgroundImage:[UIImage imageNamed:@"xx"] forState:(UIControlStateNormal)];
//    [aView addSubview:messageBtn];
//    [messageBtn addTarget:self action:@selector(actionMessage:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIBarButtonItem *myBtn = [[UIBarButtonItem alloc] initWithCustomView:aView];
//    
   self.navigationItem.rightBarButtonItem = myBtn;
    
}

- (void)addSubViews
{
    [self setnavigation];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 1;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = CGSizeMake(kScreenWidth/2-1, itemHeight);
    
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"MineCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"MineCollectionCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"MineHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MineHeaderView"];
    self.collectionView.bounces = NO;
    self.collectionView.backgroundColor = kColors(231, 231, 231, 1);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   return self.dataArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MineCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MineCollectionCell" forIndexPath:indexPath];
    cell.imageV.image = [UIImage imageNamed:self.icons[indexPath.row]];
    cell.titleLb.text = self.dataArray[indexPath.row];
    cell.bgView.backgroundColor =[UIColor whiteColor];
    cell.backgroundColor =[UIColor whiteColor];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        self.headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MineHeaderView" forIndexPath:indexPath];       
        self.headerView.nickName.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
        NSData *imageData = [[NSUserDefaults standardUserDefaults]objectForKey:@"headerImage1"];
        
         self.headerView.headImage.image = [UIImage imageWithData:imageData];
        return  self.headerView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)sectioncoll
{
    return CGSizeMake(kScreenWidth, 198*kHeightScale);
}


#pragma mark -- 按钮点击事件

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.hidesBottomBarWhenPushed = YES;
    switch (indexPath.row) {
        case 0:{
            // 跳转到速降订单
            OrderFormViewController *orderVC = [[OrderFormViewController alloc] init];
            [self.navigationController pushViewController:orderVC animated:YES];
            orderVC.pushType = 0;
            break;
        }
        case 1: {
            // 跳转到积分商城
            IntegralStore *storeVC = [[IntegralStore alloc] init];
            [self.navigationController pushViewController:storeVC animated:YES];
            break;
        }
        case 2:{
            // 跳转到我的晒单
            ShaidanViewController *shaiVC = [[ShaidanViewController alloc] init];
            [self.navigationController pushViewController:shaiVC animated:YES];
            break;
            
        }
        case 3:{
            
            // 跳转到我的钱包
            MyWallet *walletVC = [[MyWallet alloc] initWithNibName:@"MyWallet" bundle:[NSBundle mainBundle]];
            [self.navigationController pushViewController:walletVC animated:YES];
            break;
        }
//        case 4:{
//            // 跳转到收货地址
//            AddressManager *addressVC = [[AddressManager alloc] init];
//            [self.navigationController pushViewController:addressVC animated:YES];
//            break;
//        }
//        case 5:{
//            // 跳转到我的积分
//            MyIntegral *integralVC = [[MyIntegral alloc] init];
//            [self.navigationController pushViewController:integralVC animated:YES];
//            break;
//        }
            //        case 7:{
            //            // 跳转到帮助反馈
            //            HelpViewController *helpVC =[[HelpViewController alloc] init];
            //            [self.navigationController pushViewController:helpVC animated:YES];
            //            break;
            //        }
            
        default:
            break;
    }

}

//设置
- (void)actionSet:(UIButton *)button
{
    PersonCenter *personCenter = [[PersonCenter alloc]init];
    [self.navigationController pushViewController:personCenter animated:YES];
}

// 登录注册
- (void)actionLoginBtn:(UIButton *)button
{
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:loginVC];
    [self presentViewController:navi animated:YES completion:nil];
}

- (void)actionRegistBtn:(UIButton *)button
{
    InvationCode *invaVC = [[InvationCode alloc] init];
    BaseNavigationController *navi = [[BaseNavigationController alloc] initWithRootViewController:invaVC];
    [self presentViewController:navi animated:YES completion:nil];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
